//
//  baseVoiceButton.swift
//  DingDongZP
//
//  Created by Zhongnan on 16/4/16.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

import UIKit

class baseVoiceButton: UIButton {

    var gifView:LDImageView?
    var defaultGifView:UIImageView?
    var timeLab:UILabel?
    
    var path:String?
    var isPlayingGif = false {
        didSet{
            if isPlayingGif == false {
                
                _ = NSBundle.mainBundle().URLForResource("voice_default", withExtension: nil)?.absoluteString as String!
                gifView = LDImageView.init(image: UIImage.init(named: "voice_default"))//LDGIFImage.init(contentsOfFile: pathDafault!))
                
            }else {
                let path = NSBundle.mainBundle().URLForResource("voice.gif", withExtension: nil)?.absoluteString as String!
                gifView = LDImageView.init(image: LDGIFImage.init(contentsOfFile: path!))
            }
        }
    }
    
    func animaPlay(){
        defaultGifView?.hidden = true
        gifView?.startAnimating()
    }
    
    func animaStop(){
        defaultGifView?.hidden = false
        gifView?.stopAnimating()
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupDefaultContent()
        setBackGoundImg()
        
//        //添加观察
//        status = (gifView?.isAnimation)!
//        addObserver(self, forKeyPath: "status", options: NSKeyValueObservingOptions.New, context: nil)
    }
    
//    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
//        if keyPath == "status" {
//            print(object,"nofi------->")
//        }
//    }
    deinit {
//        removeObserver(self, forKeyPath: "status")
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setBackGoundImg() {
        
        var backimg = UIImage.init(named: "voice_resume_background_normal_mini")!
//        UIEdgeInsetsMake(<#T##top: CGFloat##CGFloat#>, <#T##left: CGFloat##CGFloat#>, <#T##bottom: CGFloat##CGFloat#>, <#T##right: CGFloat##CGFloat#>)
        backimg = backimg.resizableImageWithCapInsets(UIEdgeInsetsMake(6, 10, 6, 6), resizingMode: UIImageResizingMode.Stretch)
        setBackgroundImage(backimg, forState: UIControlState.Normal)
    }
    
    func setupDefaultContent() {
        let path = NSBundle.mainBundle().URLForResource("voice.gif", withExtension: nil)?.absoluteString as String!
        gifView = LDImageView.init(image: LDGIFImage.init(contentsOfFile: path!))
        
        defaultGifView = UIImageView.init(image: UIImage(named: "resumeVoiceIcon_default"))
        
        
        timeLab = UILabel.setDetailsLab("0' ", fontSize: 13, titleColor: LDFontColor.DeepGray, Alignment: .Right)
        addSubview(gifView!)
        addSubview(timeLab!)
        addSubview(defaultGifView!)
        
        defaultGifView?.snp_makeConstraints(closure: { (make) in
            make.left.equalTo(LDMargin_H*3)
            make.centerY.equalTo(self)
            make.size.equalTo(CGSizeMake(12, 12))
        })
        
        gifView?.snp_makeConstraints(closure: { (make) in
            make.left.equalTo(LDMargin_H*3)
            make.centerY.equalTo(self)
            make.size.equalTo(CGSizeMake(12, 12))
        })
        timeLab?.snp_makeConstraints(closure: { (make) in
            make.right.equalTo(-LDMargin_H*2)
            make.centerY.equalTo(self)
            make.size.equalTo(CGSizeMake(25, 12))
        })
        
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        //创建折角按钮
//        let shape:CAShapeLayer = CAShapeLayer()
//        let bepath:UIBezierPath = UIBezierPath()
//        bepath.lineWidth = 5
//
//        UIColor.redColor().set()
//        bepath.lineCapStyle = CGLineCap.Round
//        bepath.lineJoinStyle = CGLineJoin.Round
//        
//        bepath.moveToPoint(CGPoint(x: 8,y: 0))
//        bepath.addLineToPoint(CGPoint(x: 8,y: height/2 - 8))
//        bepath.addLineToPoint(CGPoint(x: 0,y: height/2))
//        bepath.addLineToPoint(CGPoint(x: 8,y: height/2 + 8 ))
//        bepath.addLineToPoint(CGPoint(x: 8,y: height))
//        bepath.addLineToPoint(CGPoint(x: width,y:height))
//        bepath.addLineToPoint(CGPoint(x: width,y: 0))
//        
//        bepath.closePath()
//        shape.path = bepath.CGPath
//        self.layer.mask = shape

    }
}
